package com.doraemon.service.impl;

import com.doraemon.context.BaseContext;
import com.doraemon.dto.ShoppingCartDTO;
import com.doraemon.entity.Dish;
import com.doraemon.entity.Setmeal;
import com.doraemon.entity.ShoppingCart;
import com.doraemon.mapper.DishMapper;
import com.doraemon.mapper.SetmealDishMapper;
import com.doraemon.mapper.ShoppingCartMapper;
import com.doraemon.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理Service实现
 **/
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /***
     * 查看购物车
     * ---
     * @param
     * @return: com.doraemon.result.Result<java.util.List < com.doraemon.entity.ShoppingCart>>
     */
    @Override
    public List<ShoppingCart> getListShoppingCart() {

        // 1.构建购物车实体
        ShoppingCart shoppingCart = new ShoppingCart();

        // 2.设置当前操作的id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 3.调用购物车Mapper层，根据购物车实体查询购物车信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        return shoppingCarts;
    }

    /***
     * 添加购物车
     * ---
     * @param shoppingCartDTO 购物车DTO类（前端参数）
     * @return: com.doraemon.result.Result
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        // 1.创建购物车实体
        ShoppingCart shoppingCart = new ShoppingCart();

        // 2.调用工具类BeanUtils，将购物车DTO类拷贝到购物车实体
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 3.设置当前操作的id（只能查询自己的购物车信息）
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 4.判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);

        // 5.如果购物车中存在该商品，更新数量+1
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            // 调用购物车Mapper层，更新数量
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {

            // 6.如果购物车中不存在该商品，插入数据，商品数量为1
            // 7.判断当前添加到购物车的商品是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) { // 当前为菜品，将菜品信息设置到购物车实体
                // 8.调用菜品Mapper层，根据菜品id查询菜品实体
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {

                // 当前为套餐，将套餐信息设置到购物车实体
                // 9.调用套餐Mapper层，根据套餐id查询套餐实体
                Setmeal setmeal = setmealDishMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            // 10.设置商品数量为1以及创建时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 11.调用购物车Mapper层，插入数据
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /***
     * 清空购物车
     * ---
     * @return: com.doraemon.result.Result
     */
    @Override
    public void cleanShoppingCart() {

        // 1.获取当前用户id
        Long userId = BaseContext.getCurrentId();

        // 2.调用购物车Mapper层，清空指定用户的购物车
        shoppingCartMapper.cleanByUserId(userId);
    }

    /***
     * 删除购物车中的指定商品
     * ---
     * @param shoppingCartDTO 购物车DTO类（前端参数）
     * @return: com.doraemon.result.Result
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        // 1.创建购物车实体
        ShoppingCart shoppingCart = new ShoppingCart();

        // 2.调用工具类BeanUtils，将购物车DTO类拷贝到购物车实体
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 3.设置当前操作的id（只能删除自己的购物车商品）
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 4.获取该商品的原来数量
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        Integer number = shoppingCarts.get(0).getNumber();

        // 5.要删除的商品数为1，则直接删除
        if (number == 1) {
            shoppingCartMapper.delete(shoppingCart);
            return;
        }

        // 6.设置该商品的id
        Long id = shoppingCarts.get(0).getId();
        shoppingCart.setId(id);

        // 7.设置要删除商品的数量（数量-1）
        shoppingCart.setNumber(number - 1);

        // 8.调用购物车Mapper层，更新商品数量，完成删除操作
        shoppingCartMapper.updateNumberById(shoppingCart);
    }
}
